﻿#ifndef __SS_DEFAULT_MALLOC_H__
#define __SS_DEFAULT_MALLOC_H__
#include "../ssBaseDefine.h"

namespace StarSeeker
{

class ssIMalloc
{
public:
	ssIMalloc() {}
	virtual ~ssIMalloc() {}
	//分配对象
	virtual void* Malloc()=0;
	//回收对象
	virtual bool Free(void* pt)=0;
	//回收所有已分配的对象
	virtual void Recovery()=0;
	//释放所有内存
	virtual void Clear() = 0;
};

template<typename T>
class ssIMallocObj
{
public:
	ssIMallocObj() {}
	virtual ~ssIMallocObj() {}
	//分配对象
	virtual T* Malloc() = 0;
	//回收对象
	virtual bool Free(T* pt) = 0;
	//回收所有已分配的对象
	virtual void Recovery() = 0;
	//释放所有内存
	virtual void Clear() = 0;
};

template<ulint MEMSIZE>
class ssDefMalloc : public ssIMalloc
{
public:
	//分配对象
	void* Malloc()
	{
		return ::operator new(MEMSIZE);
	}
	//回收对象
	bool Free(void* pt)
	{
		if (pt)
		{
			::operator delete(pt);
			return true;
		}
		return false;
	}
	void Recovery()
	{
	}
	void Clear()
	{
	}
};

template<typename T>
class ssDefMallocObject : public ssIMallocObj<T>
{
public:
	//分配对象
	T* Malloc()
	{
		return new T();
	}
	//回收对象
	bool Free(T* pt)
	{
		if (pt)
		{
			delete pt;
			return true;
		}
		return false;
	}
	void Recovery()
	{
	}
	void Clear()
	{
	}
};

}
#endif
